package com.rljk.controller.backend;/**
 * Created by mac on 2018/8/16.
 */

import com.rljk.common.controller.BaseController;
import com.rljk.common.result.CodeMsg;
import com.rljk.common.result.Result;
import com.rljk.domain.common.PageData;
import com.rljk.service.CommentInManager;
import com.rljk.service.HeadlinesManager;
import com.rljk.utils.AppUtil;
import com.rljk.utils.Tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CommentController
 * @Description 评论
 * @Author zhang
 * @Date 2018/8/16 10:14
 */
@RestController
@RequestMapping("/backcomment")
@Slf4j
public class CommentController extends BaseController {

    @Resource(name = "commentInService")
    private CommentInManager commentInService;

    @Resource(name = "headlinesService")
    private HeadlinesManager headlinesService;


    @RequestMapping(value="/alllist")
    public Object allComment(HttpServletRequest request) {
        log.info("评论接口");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        Map<String,Object> map = new HashMap<>();
        try {
            if (Tools.checkKey("ALLCOMMENT", pd.getString("FKEY"))) {
                if (AppUtil.checkParam("allComment",pd)){

                    List<PageData> newsComment = commentInService.findByNewsdetailId(pd);

                    map.put("NEWSCOMMENT",newsComment);

                    if (newsComment.size() ==0){
                        return Result.success(null);
                    }else {
                        return Result.success(map);
                    }

                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }


    /**
     * @Author zhang
     * @Description  修改评论是否显示
     * @Date 2018/8/16 13:33
     * @Param []
     * @return java.lang.Object
     **/
    @RequestMapping(value="/edit")
    public Object editIsShow() {
        log.info("修改评论是否显示");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        Map<String,Object> map = new HashMap<>();
        try {
            if (Tools.checkKey("EDITCOMMENTISSHOW", pd.getString("FKEY"))) {
                if (AppUtil.checkParam("editIsShow",pd)){

                    Integer is_show = Integer.parseInt((String) pd.get("IS_SHOW"));
                    commentInService.edit(pd);

                    log.info(pd.toString());

                    //pd.remove("");

                    //查询该评论所属的新闻
                    PageData commentNum = commentInService.findCommentNumByCommentId(pd);
                    pd.put("HEADLINES_ID",commentNum.get("HEADLINES_ID"));
                    Integer num = (Integer)commentNum.get("COMMENT_NUM");

                    log.info(num.toString());

                    if (is_show == 1){
                        pd.put("COMMENT_NUM",num + 1);
                    }else {
                        pd.put("COMMENT_NUM",num - 1);
                    }

                    log.info(pd.toString());
                    headlinesService.edit(pd);

                    return Result.success(null);
                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }


}
